/*
 * 版权所有 Amor. 保留所有权利。
 * 根据 MIT 许可证条款分发。
 *
 * 作者： amor
 *		Ant Design 实现
 */

/*! AntControlLook - Haiku 的 Ant Design 5.0 风格 */

#include "AntControlLook.h"

#include <ControlLook.h>
#include <GradientLinear.h>
#include <Rect.h>
#include <Region.h>
#include <View.h>
#include <WindowPrivate.h>
#include <stdio.h>

#include <algorithm>
#include <cmath>
#include <new>

namespace BPrivate {

// Ant Design 5.0 特定的UI常量定义
// 这些常量用于控制Ant Design风格UI元素的视觉表现

// 边框高亮部分的色调值（1.0表示不改变原色）
static const float kEdgeBevelLightTint = 1.0;
// 边框阴影部分的色调值（1.0表示不改变原色）
static const float kEdgeBevelShadowTint = 1.0;
// 鼠标悬停时的色调变化因子（0.55表示轻微变亮）
static const float kHoverTintFactor = 0.55;

// UI元素的圆角半径（6.0f表示中等圆角，符合Ant Design风格）
static const float kRadius = 6.0f;
// 按钮弹出指示器的宽度（11像素）
static const float kButtonPopUpIndicatorWidth = 11;

// Ant Design 主色系定义
// 主色 - Ant Design蓝色 (#1890FF)
static const rgb_color kAntPrimaryColor = {24, 144, 255};
// 成功色 - Ant Design绿色 (#52C41A)
static const rgb_color kAntSuccessColor = {82, 196, 26};
// 警告色 - Ant Design金色 (#FAAD14)
static const rgb_color kAntWarningColor = {250, 173, 20};
// 错误色 - Ant Design红色 (#F5222D)
static const rgb_color kAntErrorColor = {245, 34, 45};
// 信息色 - Ant Design蓝色 (#1890FF)
static const rgb_color kAntInfoColor = {24, 144, 255};

// 构造函数 - 继承HaikuControlLook基类
AntControlLook::AntControlLook() : HaikuControlLook() {}

// 析构函数
AntControlLook::~AntControlLook() {}

// #pragma mark -

// 绘制按钮边框的基础方法
// 使用默认圆角半径(kRadius)绘制Ant Design风格的按钮边框
// 参数:
//   view - 绘制目标视图
//   rect - 按钮的矩形区域(会被修改)
//   updateRect - 需要更新的区域
//   base - 基础颜色
//   background - 背景颜色
//   flags - 按钮状态标志(B_DISABLED, B_HOVER等)
//   borders - 需要绘制的边框(B_LEFT_BORDER等)
void AntControlLook::DrawButtonFrame(BView *view, BRect &rect,
                                     const BRect &updateRect,
                                     const rgb_color &base,
                                     const rgb_color &background, uint32 flags,
                                     uint32 borders) {
  // 调用内部实现方法，使用默认圆角半径
  _DrawButtonFrame(view, rect, updateRect, kRadius, kRadius, kRadius, kRadius,
                   base, background, 1.0, 1.0, flags, borders);
}

// 绘制按钮边框的变体方法
// 允许自定义圆角半径，其他参数与基础方法相同
// 参数:
//   radius - 统一的圆角半径
//   其他参数同上
void AntControlLook::DrawButtonFrame(BView *view, BRect &rect,
                                     const BRect &updateRect, float radius,
                                     const rgb_color &base,
                                     const rgb_color &background, uint32 flags,
                                     uint32 borders) {
  // 调用内部实现方法，使用统一的圆角半径
  _DrawButtonFrame(view, rect, updateRect, radius, radius, radius, radius, base,
                   background, 1.0, 1.0, flags, borders);
}

void AntControlLook::DrawButtonFrame(
    BView *view, BRect &rect, const BRect &updateRect, float leftTopRadius,
    float rightTopRadius, float leftBottomRadius, float rightBottomRadius,
    const rgb_color &base, const rgb_color &background, uint32 flags,
    uint32 borders) {
  _DrawButtonFrame(view, rect, updateRect, leftTopRadius, rightTopRadius,
                   leftBottomRadius, rightBottomRadius, base, background, 1.0,
                   1.0, flags, borders);
}

void AntControlLook::DrawButtonBackground(BView *view, BRect &rect,
                                          const BRect &updateRect,
                                          const rgb_color &base, uint32 flags,
                                          uint32 borders,
                                          orientation orientation) {
  _DrawButtonBackground(view, rect, updateRect, kRadius, kRadius, kRadius,
                        kRadius, base, false, flags, borders, orientation);
}

void AntControlLook::DrawButtonBackground(BView *view, BRect &rect,
                                          const BRect &updateRect, float radius,
                                          const rgb_color &base, uint32 flags,
                                          uint32 borders,
                                          orientation orientation) {
  _DrawButtonBackground(view, rect, updateRect, radius, radius, radius, radius,
                        base, false, flags, borders, orientation);
}

void AntControlLook::DrawButtonBackground(
    BView *view, BRect &rect, const BRect &updateRect, float leftTopRadius,
    float rightTopRadius, float leftBottomRadius, float rightBottomRadius,
    const rgb_color &base, uint32 flags, uint32 borders,
    orientation orientation) {
  _DrawButtonBackground(view, rect, updateRect, leftTopRadius, rightTopRadius,
                        leftBottomRadius, rightBottomRadius, base, false, flags,
                        borders, orientation);
}

// 绘制菜单栏背景
// 实现Ant Design风格的菜单栏背景，具有扁平化设计和微妙的阴影效果
// 参数:
//   view - 绘制目标视图
//   rect - 菜单栏矩形区域(会被修改)
//   updateRect - 需要更新的区域
//   base - 基础颜色
//   flags - 状态标志(B_DISABLED禁用状态, B_FOCUSED焦点状态等)
//   borders - 需要绘制的边框(B_TOP_BORDER等)
void AntControlLook::DrawMenuBarBackground(BView *view, BRect &rect,
                                           const BRect &updateRect,
                                           const rgb_color &base, uint32 flags,
                                           uint32 borders) {
  // 检查是否需要绘制，避免不必要的绘制操作
  if (!ShouldDraw(view, rect, updateRect)) return;

  // Ant Design菜单栏使用扁平化设计，带有微妙的阴影效果
  // 定义渐变色调
  float topTint = 1.0;     // 顶部色调值
  float bottomTint = 1.0;  // 底部色调值

  rgb_color customColor = base;                // 使用基础颜色作为起点
  bool isEnabled = (flags & B_DISABLED) == 0;  // 是否启用状态
  bool isFocused = (flags & B_FOCUSED) != 0;   // 是否获得焦点

  if (isEnabled || isFocused) {
    // 启用或获得焦点状态下的颜色处理
    customColor = tint_color(ui_color(B_WINDOW_TAB_COLOR), 1.0);
    rgb_color bevelColor1 = tint_color(customColor, 1.0);  // 斜面颜色1
    rgb_color bevelColor2 = tint_color(customColor, 1.0);  // 斜面颜色2

    topTint = 1.0;
    bottomTint = 1.0;

    // 绘制框架，使用相同的颜色使边框看起来更扁平
    _DrawFrame(view, rect, bevelColor1, bevelColor1, bevelColor2, bevelColor2,
               borders & B_TOP_BORDER);
  } else {
    // 禁用状态下的颜色处理
    rgb_color cornerColor = tint_color(customColor, 1.0);    // 角落颜色
    rgb_color bevelColorTop = tint_color(customColor, 1.0);  // 顶部斜面颜色
    rgb_color bevelColorLeft = tint_color(customColor, 1.0);  // 左侧斜面颜色
    rgb_color bevelColorRightBottom =
        tint_color(customColor, 1.0);  // 右下方斜面颜色

    topTint = 1.0;
    bottomTint = 1.0;

    // 绘制框架，使用相同的颜色使边框看起来更扁平
    _DrawFrame(view, rect, bevelColorLeft, bevelColorTop, bevelColorRightBottom,
               bevelColorRightBottom, cornerColor, cornerColor, borders);
  }

  // 绘制顶部表面，使用渐变填充
  _FillGradient(view, rect, customColor, topTint, bottomTint);
}

void AntControlLook::DrawMenuFieldFrame(BView *view, BRect &rect,
                                        const BRect &updateRect,
                                        const rgb_color &base,
                                        const rgb_color &background,
                                        uint32 flags, uint32 borders) {
  _DrawButtonFrame(view, rect, updateRect, kRadius, kRadius, kRadius, kRadius,
                   base, background, 1.0, 1.0, flags, borders);
}